package com.desire.controller.sys;

import com.desire.bean.common.PageBean;
import com.desire.bean.common.ResultBean;
import com.desire.bean.sys.User;
import com.desire.bean.sys.UserBatchBean;
import com.desire.common.validation.group.Modify;
import com.desire.common.validation.group.Save;
import com.desire.controller.common.BaseController;
import com.desire.service.sys.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * @author LittleDesire
 * @description
 * @date 2022-05-06 周五 16:15
 * @since 17
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/api/user")
@Slf4j
public class UserController extends BaseController {
    @Autowired
    UserService userService;


    @PostMapping("/query")
    @ApiOperation(value = "查询用户数据")
    public ResultBean query(@RequestBody Map map) {
        PageBean pageBean = userService.query(map);
        return success(pageBean);
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增用户数据", notes = "添加用户，会返回msg")
    public ResultBean save(@RequestBody @Validated(Save.class) User user) {
        int rs = userService.save(user);
        return rs > 0 ? success("保存成功") : success("保存失败");
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改用户数据")
    public ResultBean update(@RequestBody @Validated(Modify.class) User user) {
        int rs = userService.update(user);
        return rs > 0 ? success("修改成功") : success("修改失败");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除用户数据")
    public ResultBean delete(String id) {
        int rs = userService.delete(id);
        return rs > 0 ? success("删除成功") : success("删除失败");
    }

    @PostMapping("/batch")
    @ApiOperation(value = "批量操作用户数据", notes = "批量删除，禁用，启用")
    public ResultBean batchUpdate(@Valid @RequestBody UserBatchBean userBatchBean) {
        int ints = userService.batchUpdate(userBatchBean);
        return ints > 0 ? success("批量操作成功", ints) : success("批量操作失败", ints);
    }
}







